package com.example.wan1.look;

import android.util.Log;

import com.example.wan1.base.BasePresenter;
import com.example.wan1.data.BaseObserver;
import com.example.wan1.data.ResponseList;
import com.example.wan1.data.RetrofitUtil;
import com.example.wan1.data.RxSchedulers;
import com.example.wan1.model.ArticleBean;

/***
 * 记录一个傻逼的问题 Annotation processors must be explicitly declared now..
 * .compose(RxSchedulers.observableIO2Main())还有就是butternife的使用
 * 必须加上jdk8的引用
 */

public class LookPresenter extends BasePresenter<LookContract.View> implements LookContract.Presenter {

    private static final String TAG = "LookPresenter";

    @Override
    public void start() {

    }

    @Override
    public void getData() {

        Log.i(TAG, "getData: " + (isViewAttached()));
        if (isViewAttached()) {
//            getView().showLoading();

            // TODO: 2020/5/1 更改Observer的参数，改成通用的了

//            RetrofitUtil.getInstance().getArticleList(0)
//                    .compose(RxSchedulers.observableIO2Main())
//                    .subscribe(new Observer<LookBean>() {
//                        @Override
//                        public void onSubscribe(Disposable d) {
//
//                        }
//
//                        @Override
//                        public void onNext(LookBean lookBean) {
//                            getView().onSuccess(lookBean);
//                        }
//
//                        @Override
//                        public void onError(Throwable e) {
//                            getView().onFailure("获取数据失败!");
//                        }
//
//                        @Override
//                        public void onComplete() {
//                            Log.i(TAG, "onComplete: 完成");
//                        }
//                    });

            /*RetrofitUtil.getInstance().getArticleList1(0)
                    .compose(RxSchedulers.observableIO2Main())
                    .subscribe(new Observer<ResponseBody<ResponseList<ArticleBean>>>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(ResponseBody<ResponseList<ArticleBean>> responseListResponseBody) {
                            if (responseListResponseBody.getErrorCode() == 0) {
                                //请求成功
                                ResponseList<ArticleBean> data = responseListResponseBody.getData();
                                if (data.getTotal() == 0) {
                                    Log.e(TAG, "empty data");
                                } else {
                                    getView().onSuccess(data);
                                }
                            }

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });*/

            //简化：使用baseObserver来简化操作
            RetrofitUtil.getInstance().getArticleList1(0).compose(RxSchedulers.observableIO2Main())
                    .subscribe(new BaseObserver<ResponseList<ArticleBean>>() {
                        @Override
                        public void onSuccess(ResponseList<ArticleBean> result) {
                            getView().onSuccess(result);
                        }

                        @Override
                        public void onFailure(Throwable e, String errorMsg) {
                            getView().onFailure(errorMsg);
                        }
                    });

        }
    }
}
